// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <pollux/common/compression/compression.h>
#include <pollux/common/base/exceptions.h>
#include <melon/container/f14_map.h>
#include <melon/conv.h>

namespace kumo::pollux::common {
    std::unique_ptr<melon::compression::Codec> compressionKindToCodec(
        CompressionKind kind) {
        switch (static_cast<int32_t>(kind)) {
            case CompressionKind_NONE:
                return getCodec(melon::compression::CodecType::NO_COMPRESSION);
            case CompressionKind_ZLIB:
                return getCodec(melon::compression::CodecType::ZLIB);
            case CompressionKind_SNAPPY:
                return getCodec(melon::compression::CodecType::SNAPPY);
            case CompressionKind_ZSTD:
                return getCodec(melon::compression::CodecType::ZSTD);
            case CompressionKind_LZ4:
                return getCodec(melon::compression::CodecType::LZ4);
            case CompressionKind_GZIP:
                return getCodec(melon::compression::CodecType::GZIP);
            default:
                POLLUX_UNSUPPORTED(
                    "Not support {} in melon", compressionKindToString(kind));
        }
    }

    CompressionKind codecTypeToCompressionKind(melon::compression::CodecType type) {
        switch (type) {
            case melon::compression::CodecType::NO_COMPRESSION:
                return CompressionKind_NONE;
            case melon::compression::CodecType::ZLIB:
                return CompressionKind_ZLIB;
            case melon::compression::CodecType::SNAPPY:
                return CompressionKind_SNAPPY;
            case melon::compression::CodecType::ZSTD:
                return CompressionKind_ZSTD;
            case melon::compression::CodecType::LZ4:
                return CompressionKind_LZ4;
            case melon::compression::CodecType::GZIP:
                return CompressionKind_GZIP;
            default:
                POLLUX_UNSUPPORTED(
                    "Not support melon codec type {}", melon::to<std::string>(type));
        }
    }

    std::string compressionKindToString(CompressionKind kind) {
        switch (static_cast<int32_t>(kind)) {
            case CompressionKind_NONE:
                return "none";
            case CompressionKind_ZLIB:
                return "zlib";
            case CompressionKind_SNAPPY:
                return "snappy";
            case CompressionKind_LZO:
                return "lzo";
            case CompressionKind_ZSTD:
                return "zstd";
            case CompressionKind_LZ4:
                return "lz4";
            case CompressionKind_GZIP:
                return "gzip";
        }
        return melon::to<std::string>("unknown - ", kind);
    }

    CompressionKind stringToCompressionKind(const std::string &kind) {
        static const melon::F14FastMap<std::string, CompressionKind>
                stringToCompressionKindMap = {
                    {"none", CompressionKind_NONE},
                    {"zlib", CompressionKind_ZLIB},
                    {"snappy", CompressionKind_SNAPPY},
                    {"lzo", CompressionKind_LZO},
                    {"zstd", CompressionKind_ZSTD},
                    {"lz4", CompressionKind_LZ4},
                    {"gzip", CompressionKind_GZIP}
                };
        auto iter = stringToCompressionKindMap.find(kind);
        if (iter != stringToCompressionKindMap.end()) {
            return iter->second;
        } else {
            POLLUX_UNSUPPORTED("Not support compression kind {}", kind);
        }
    }
} // namespace kumo::pollux::common
